package com.jivam.lottery.main;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import com.jivam.lottery.data.DataCalculator;
import com.jivam.lottery.data.DataImport;
import com.jivam.lottery.data.DataUtil;
import com.jivam.lottery.model.SelectNum;
import com.jivam.lottery.util.Constants;

/**
 * 彩票算码工具主面板类
 * @author Jivam
 */
public class Calculate extends JFrame implements ActionListener{

	private static final long serialVersionUID = -5629290145965358626L;
	//预测范围
	private JTextPane periodJtp = new JTextPane();//开奖期数
	private JTextPane sum4MinJtp = new JTextPane();//最小和值
	private JTextPane sum4MaxJtp = new JTextPane();//最大和值
	
	private JTextPane aJtp = new JTextPane();
	private JTextPane bJtp = new JTextPane();
	private JTextPane cJtp = new JTextPane();
	private JTextPane dJtp = new JTextPane();
	private JTextPane eJtp = new JTextPane();
	private JTextPane fJtp = new JTextPane();
	private JTextPane gJtp = new JTextPane();

	//两数和数
	private JTextPane abSumJtp = new JTextPane();
	private JTextPane acSumJtp = new JTextPane();
	private JTextPane adSumJtp = new JTextPane();
	private JTextPane aeSumJtp = new JTextPane();
	private JTextPane afSumJtp = new JTextPane();
	private JTextPane agSumJtp = new JTextPane();
	private JTextPane bcSumJtp = new JTextPane();
	private JTextPane bdSumJtp = new JTextPane();
	private JTextPane beSumJtp = new JTextPane();
	private JTextPane bfSumJtp = new JTextPane();
	private JTextPane bgSumJtp = new JTextPane();
	private JTextPane cdSumJtp = new JTextPane();
	private JTextPane ceSumJtp = new JTextPane();
	private JTextPane cfSumJtp = new JTextPane();
	private JTextPane cgSumJtp = new JTextPane();
	private JTextPane deSumJtp = new JTextPane();
	private JTextPane dfSumJtp = new JTextPane();
	private JTextPane dgSumJtp = new JTextPane();
	private JTextPane efSumJtp = new JTextPane();
	private JTextPane egSumJtp = new JTextPane();
	private JTextPane fgSumJtp = new JTextPane();
	
	//两数见数
	private JTextPane abAppearJtp = new JTextPane();
	private JTextPane acAppearJtp = new JTextPane();
	private JTextPane adAppearJtp = new JTextPane();
	private JTextPane aeAppearJtp = new JTextPane();
	private JTextPane afAppearJtp = new JTextPane();
	private JTextPane agAppearJtp = new JTextPane();
	private JTextPane bcAppearJtp = new JTextPane();
	private JTextPane bdAppearJtp = new JTextPane();
	private JTextPane beAppearJtp = new JTextPane();
	private JTextPane bfAppearJtp = new JTextPane();
	private JTextPane bgAppearJtp = new JTextPane();
	private JTextPane cdAppearJtp = new JTextPane();
	private JTextPane ceAppearJtp = new JTextPane();
	private JTextPane cfAppearJtp = new JTextPane();
	private JTextPane cgAppearJtp = new JTextPane();
	private JTextPane deAppearJtp = new JTextPane();
	private JTextPane dfAppearJtp = new JTextPane();
	private JTextPane dgAppearJtp = new JTextPane();
	private JTextPane efAppearJtp = new JTextPane();
	private JTextPane egAppearJtp = new JTextPane();
	private JTextPane fgAppearJtp = new JTextPane();
	
	//单重数 对数 兄弟数
	private JCheckBox zccm = new JCheckBox("只除重码");
	private JCheckBox zcec = new JCheckBox("只除二重");
	private JCheckBox zcsc = new JCheckBox("只除三重");
	private JCheckBox wds = new JCheckBox("无对数");
	private JCheckBox wxds = new JCheckBox("无兄弟数");
	
	private JCheckBox zycm = new JCheckBox("只要重码");
	private JCheckBox zyec = new JCheckBox("只要二重");
	private JCheckBox zysc = new JCheckBox("只要三重");
	private JCheckBox yds = new JCheckBox("有对数");
	private JCheckBox yxds = new JCheckBox("有兄弟数");
	
	//多码和值
	private JTextPane abcSumJtp = new JTextPane();
	private JTextPane abdSumJtp = new JTextPane();
	private JTextPane acdSumJtp = new JTextPane();
	private JTextPane bcdSumJtp = new JTextPane();
	private JTextPane abcdSumJtp = new JTextPane();
	private JTextPane doubleSumJtp = new JTextPane();//两数合
	private JTextPane spanJtp = new JTextPane();//跨度值
	private JCheckBox spanBuyJcb = new JCheckBox("购买");
	private JCheckBox adMatchJcb = new JCheckBox("头尾不配");
	private JCheckBox adMatchBuyJcb = new JCheckBox("购买");
	
	//草稿
	private JTextPane aDraftJtp1 = new JTextPane();
	private JTextPane bDraftJtp1 = new JTextPane();
	private JTextPane cDraftJtp1 = new JTextPane();
	private JTextPane dDraftJtp1 = new JTextPane();
	private JTextPane eDraftJtp1 = new JTextPane();
	private JTextPane fDraftJtp1 = new JTextPane();
	private JTextPane gDraftJtp1 = new JTextPane();
	
	private JTextPane aDraftJtp2 = new JTextPane();
	private JTextPane bDraftJtp2 = new JTextPane();
	private JTextPane cDraftJtp2 = new JTextPane();
	private JTextPane dDraftJtp2 = new JTextPane();
	private JTextPane eDraftJtp2 = new JTextPane();
	private JTextPane fDraftJtp2 = new JTextPane();
	private JTextPane gDraftJtp2 = new JTextPane();
	
	private JTextPane abDraftJtp1 = new JTextPane();
	private JTextPane acDraftJtp1 = new JTextPane();
	private JTextPane adDraftJtp1 = new JTextPane();
	private JTextPane aeDraftJtp1 = new JTextPane();
	private JTextPane afDraftJtp1 = new JTextPane();
	private JTextPane agDraftJtp1 = new JTextPane();
	private JTextPane bcDraftJtp1 = new JTextPane();
	private JTextPane bdDraftJtp1 = new JTextPane();
	private JTextPane beDraftJtp1 = new JTextPane();
	private JTextPane bfDraftJtp1 = new JTextPane();
	private JTextPane bgDraftJtp1 = new JTextPane();
	private JTextPane cdDraftJtp1 = new JTextPane();
	private JTextPane ceDraftJtp1 = new JTextPane();
	private JTextPane cfDraftJtp1 = new JTextPane();
	private JTextPane cgDraftJtp1 = new JTextPane();
	private JTextPane deDraftJtp1 = new JTextPane();
	private JTextPane dfDraftJtp1 = new JTextPane();
	private JTextPane dgDraftJtp1 = new JTextPane();
	private JTextPane efDraftJtp1 = new JTextPane();
	private JTextPane egDraftJtp1 = new JTextPane();
	private JTextPane fgDraftJtp1 = new JTextPane();
	
	private JTextPane abDraftJtp2 = new JTextPane();
	private JTextPane acDraftJtp2 = new JTextPane();
	private JTextPane adDraftJtp2 = new JTextPane();
	private JTextPane aeDraftJtp2 = new JTextPane();
	private JTextPane afDraftJtp2 = new JTextPane();
	private JTextPane agDraftJtp2 = new JTextPane();
	private JTextPane bcDraftJtp2 = new JTextPane();
	private JTextPane bdDraftJtp2 = new JTextPane();
	private JTextPane beDraftJtp2 = new JTextPane();
	private JTextPane bfDraftJtp2 = new JTextPane();
	private JTextPane bgDraftJtp2 = new JTextPane();
	private JTextPane cdDraftJtp2 = new JTextPane();
	private JTextPane ceDraftJtp2 = new JTextPane();
	private JTextPane cfDraftJtp2 = new JTextPane();
	private JTextPane cgDraftJtp2 = new JTextPane();
	private JTextPane deDraftJtp2 = new JTextPane();
	private JTextPane dfDraftJtp2 = new JTextPane();
	private JTextPane dgDraftJtp2 = new JTextPane();
	private JTextPane efDraftJtp2 = new JTextPane();
	private JTextPane egDraftJtp2 = new JTextPane();
	private JTextPane fgDraftJtp2 = new JTextPane();
	
	//结果显示
	private JTextArea text = new JTextArea(18,35);
	
	private JButton bit = new JButton("位数");
	private JButton sum = new JButton("合数");
	private JButton appear = new JButton("见数");
	private JButton calculator = new JButton("算码");
	private JButton clear = new JButton("清除");
	private JButton flash = new JButton("刷新");
	private JButton save = new JButton("保存");
	private JButton view = new JButton("查看");
	
	
	private JTextField zmy = new JTextField(4);//主码
	private JTextField fmy = new JTextField(4);//防码
	
	Map<String,JTextPane> Jtps = new HashMap<String, JTextPane>();
	
	private static final String NUMALL = "0123456789";
	
	private static final Border jtpBorder = BorderFactory.createLineBorder(Color.BLACK);

	private static final Dimension jtpSize_30 = new Dimension(30,30);
	private static final Dimension jtpSize_50 = new Dimension(50,30);
	private static final Dimension jtpSize_100 = new Dimension(100,30);
	private static final Dimension jtpSize_110 = new Dimension(125,30);

	private static final Dimension draftSize_50 = new Dimension(50,28);
	private static final Dimension draftSize_110 = new Dimension(110,28);

	private static final Dimension d1 = new Dimension(420,220);//条件面板的尺寸
	private static final Dimension d2 = new Dimension(420,130);//多码和值的尺寸
	
	private static final Font jtpFont = new Font("Consolas",Font.PLAIN, 17);
	
	private static final Color[] colorLv = new Color[]{new Color(135,206,250),Color.BLACK,Color.RED,Color.ORANGE,Color.GREEN};
	
	
	private static final DecimalFormat DF = new DecimalFormat("0.0");
	
	private static Calculate calculate;
	public static Calculate getInstanse(){
		if(calculate == null){
			calculate = new Calculate();
		}
		return calculate;
	}
	
	
	private Calculate() {
		super(Constants.TOOL_CALCULATE+Constants.VERSION+Constants.AUTHOR_NAME);
		init();
		addEventHandle();
	}

	
	private void init() {
		JPanel input = new JPanel();
		input.setLayout(new BoxLayout(input, BoxLayout.Y_AXIS));
		JPanel periodPanel = new JPanel();
		periodPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(" 预测范围"),
                BorderFactory.createEmptyBorder()));
		periodPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		initJtpInPanel(periodPanel, "  A", aJtp, jtpSize_100);
		initJtpInPanel(periodPanel, " B", bJtp, jtpSize_100);
		initJtpInPanel(periodPanel, " C", cJtp, jtpSize_100);
		initJtpInPanel(periodPanel, "  D", dJtp, jtpSize_100);
		initJtpInPanel(periodPanel, " E", eJtp, jtpSize_100);
		initJtpInPanel(periodPanel, " F", fJtp, jtpSize_100);
		initJtpInPanel(periodPanel, "  G", gJtp, jtpSize_100);
		
		initJtpInPanel(periodPanel, "开奖期数", null,periodJtp, jtpSize_50);
		initJtpInPanel(periodPanel, "总值从", null, sum4MinJtp, jtpSize_30);
		initJtpInPanel(periodPanel, "到", null, sum4MaxJtp, jtpSize_30);
		
		input.add(periodPanel);
		
		JTabbedPane conditionPane = new JTabbedPane();
		conditionPane.setPreferredSize(d1);
		
		JPanel appearPanel = new JPanel();
		appearPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		initJtpInPanel(appearPanel, "ABS", abAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "ACS", acAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "ADS", adAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "AES", aeAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "AFS", afAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "AGS", agAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "BCS", bcAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "BDS", bdAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "BES", beAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "BFS", bfAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "BGS", bgAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "CDS", cdAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "CES", ceAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "CFS", cfAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "CGS", cgAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "DES", deAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "DFS", dfAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "DGS", dgAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "EFS", efAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "EGS", egAppearJtp, jtpSize_100);
		initJtpInPanel(appearPanel, "FGS", fgAppearJtp, jtpSize_100);
		
		conditionPane.add("两数见数",appearPanel);
//		
		JPanel sumPanel = new JPanel();
		sumPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		initJtpInPanel(sumPanel, "ABH", abSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "ACH", acSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "ADH", adSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "AEH", aeSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "AFH", afSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "AGH", agSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "BCH", bcSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "BDH", bdSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "BEH", beSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "BFH", bfSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "BGH", bgSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "CDH", cdSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "CEH", ceSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "CFH", cfSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "CGH", cgSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "DEH", deSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "DFH", dfSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "DGH", dgSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "EFH", efSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "EGH", egSumJtp, jtpSize_100);
		initJtpInPanel(sumPanel, "FGH", fgSumJtp, jtpSize_100);
		
		conditionPane.add("两数和数",sumPanel);
		
		
		JPanel oldPanel = new JPanel();
		oldPanel.setLayout(new BoxLayout(oldPanel, BoxLayout.Y_AXIS));
		
		JPanel dcds = new JPanel();
		dcds.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("单重数对数兄弟数"),
                BorderFactory.createEmptyBorder()));
		dcds.setLayout(new FlowLayout());

		dcds.add(zccm);
		dcds.add(zcec);
		dcds.add(zcsc);
		dcds.add(wds);
		dcds.add(wxds);
		dcds.add(zycm);
		dcds.add(zyec);
		dcds.add(zysc);
		dcds.add(yds);
		dcds.add(yxds);
		
		oldPanel.add(dcds);
		
		JPanel dmhz = new JPanel();
		dmhz.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("多码和值"),
                BorderFactory.createEmptyBorder()));
		dmhz.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		initJtpInPanel(dmhz, " ABCH", abcSumJtp, jtpSize_110);
		initJtpInPanel(dmhz, " ABDH", abdSumJtp, jtpSize_110);
		initJtpInPanel(dmhz, " ACDH", acdSumJtp, jtpSize_110);
		initJtpInPanel(dmhz, " BCDH", bcdSumJtp, jtpSize_110);
		initJtpInPanel(dmhz, "ABCDH", abcdSumJtp, jtpSize_110);
		initJtpInPanel(dmhz, "两数和值", null, doubleSumJtp, jtpSize_110);
		initJtpInPanel(dmhz, " 跨度值", null, spanJtp, jtpSize_110);
		dmhz.add(spanBuyJcb);
		dmhz.add(adMatchJcb);
		dmhz.add(adMatchBuyJcb);
		dmhz.setPreferredSize(d2);
		oldPanel.add(dmhz);
		conditionPane.add("四位条件",oldPanel);

		JPanel draft1 = new JPanel();
		draft1.setLayout(new BoxLayout(draft1, BoxLayout.Y_AXIS));
		draft1.add(initDraftJtpInPanel(" A ", aDraftJtp1, aDraftJtp2));
		draft1.add(initDraftJtpInPanel(" B ", bDraftJtp1, bDraftJtp2));
		draft1.add(initDraftJtpInPanel(" C ", cDraftJtp1, cDraftJtp2));
		draft1.add(initDraftJtpInPanel(" D ", dDraftJtp1, dDraftJtp2));
		draft1.add(initDraftJtpInPanel(" E ", eDraftJtp1, eDraftJtp2));
		draft1.add(initDraftJtpInPanel(" F ", fDraftJtp1, fDraftJtp2));
		JPanel buttonPanel = initDraftJtpInPanel(" G ", gDraftJtp1, gDraftJtp2);
		buttonPanel.add(bit);
		draft1.add(buttonPanel);
		conditionPane.add("位数草稿",draft1);
		
		JPanel draft2 = new JPanel();
		draft2.setLayout(new GridLayout(11,2));
		draft2.add(initDraftJtpInPanel("AB", abDraftJtp1, abDraftJtp2));
		draft2.add(initDraftJtpInPanel("AC", acDraftJtp1, acDraftJtp2));
		draft2.add(initDraftJtpInPanel("AD", adDraftJtp1, adDraftJtp2));
		draft2.add(initDraftJtpInPanel("AE", aeDraftJtp1, aeDraftJtp2));
		draft2.add(initDraftJtpInPanel("AF", afDraftJtp1, afDraftJtp2));
		draft2.add(initDraftJtpInPanel("AG", agDraftJtp1, agDraftJtp2));
		draft2.add(initDraftJtpInPanel("BC", bcDraftJtp1, bcDraftJtp2));
		draft2.add(initDraftJtpInPanel("BD", bdDraftJtp1, bdDraftJtp2));
		draft2.add(initDraftJtpInPanel("BE", beDraftJtp1, beDraftJtp2));
		draft2.add(initDraftJtpInPanel("BF", bfDraftJtp1, bfDraftJtp2));
		draft2.add(initDraftJtpInPanel("BG", bgDraftJtp1, bgDraftJtp2));
		draft2.add(initDraftJtpInPanel("CD", cdDraftJtp1, cdDraftJtp2));
		draft2.add(initDraftJtpInPanel("CE", ceDraftJtp1, ceDraftJtp2));
		draft2.add(initDraftJtpInPanel("CF", cfDraftJtp1, cfDraftJtp2));
		draft2.add(initDraftJtpInPanel("CG", cgDraftJtp1, cgDraftJtp2));
		draft2.add(initDraftJtpInPanel("DE", deDraftJtp1, deDraftJtp2));
		draft2.add(initDraftJtpInPanel("DF", dfDraftJtp1, dfDraftJtp2));
		draft2.add(initDraftJtpInPanel("DG", dgDraftJtp1, dgDraftJtp2));
		draft2.add(initDraftJtpInPanel("EF", efDraftJtp1, efDraftJtp2));
		draft2.add(initDraftJtpInPanel("EG", egDraftJtp1, egDraftJtp2));
		draft2.add(initDraftJtpInPanel("FG", fgDraftJtp1, fgDraftJtp2));
		buttonPanel = new JPanel();
		buttonPanel.add(appear);
		buttonPanel.add(sum);
		draft2.add(buttonPanel);
		conditionPane.add("合见草稿", draft2);
		
		
		input.add(conditionPane);
		
		JPanel resultP = new JPanel();
		resultP.setLayout(new BoxLayout(resultP, BoxLayout.Y_AXIS));
		JPanel result = new JPanel();
		result.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("结果显示"),
                BorderFactory.createEmptyBorder()));
		this.setLayout(new GridLayout(3,1));
		text.setLineWrap(true);
		JScrollPane jsp = new JScrollPane(text);
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		result.add(jsp);
		
		JPanel mys = new JPanel();
		mys.setLayout(new FlowLayout());
		mys.add(new JLabel("主码每注："));
		mys.add(zmy);
		mys.add(new JLabel("元"));
		mys.add(new JLabel("防码每注："));
		mys.add(fmy);
		mys.add(new JLabel("元"));
		result.add(mys);
		
		JPanel btns = new JPanel();
		btns.setLayout(new FlowLayout());
		btns.add(calculator);
		btns.add(clear);
		btns.add(flash);
		btns.add(save);
		save.setEnabled(false);
		btns.add(view);
		result.add(btns);
		resultP.add(result);
		
		this.setLayout(new GridLayout(1,2));
		this.add(input);
		this.add(resultP);
		
		this.setSize(840,460);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();//居中
		this.setLocation((screenSize.width-getWidth())/2,(screenSize.height-getHeight())/2);
//		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);//关闭按钮
		flash();
	}

	private void initJtpInPanel(JPanel p,String label,Font labelFont,JTextPane jtp,Dimension jtpSize){
		initJtpInPanel(p, label, null, jtp, jtpFont, jtpSize);
	}

	private void initJtpInPanel(JPanel p,String label,JTextPane jtp,Dimension jtpSize){
		initJtpInPanel(p, label, jtpFont, jtp, jtpFont, jtpSize);
	}

	private void initJtpInPanel(JPanel p,String label,Font labelFont,JTextPane jtp,Font jtpFont,Dimension jtpSize){
		final JLabel j = new JLabel(label);
		if(labelFont != null){
			j.setFont(labelFont);
		}
		j.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		j.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String key = j.getText().trim();
				String title = key+"公式详情";
				String content = key+" = "+DataCalculator.getMappingRelation(key);
				JOptionPane.showMessageDialog(Analyse.getInstanse(),content,title, JOptionPane.INFORMATION_MESSAGE);
			}
		});
		p.add(j);
		jtp.setPreferredSize(jtpSize);
		jtp.setFont(jtpFont);
		jtp.setBorder(jtpBorder);
		p.add(jtp);
		addJtpList(label,jtp);
	}

	private JPanel initDraftJtpInPanel(String label,JTextPane jtp1,JTextPane jtp2){
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout(FlowLayout.LEFT));
		initDraftJtpInPanel(p, label, jtpFont, jtp1, jtp2, jtpFont);
		return p;
	}

	private void initDraftJtpInPanel(JPanel p,String label,Font labelFont,JTextPane jtp1,JTextPane jtp2,Font jtpFont){
		JLabel j = new JLabel(label);
		if(labelFont != null){
			j.setFont(labelFont);
		}
		p.add(j);
		jtp1.setFont(jtpFont);
		jtp1.setPreferredSize(draftSize_50);
		jtp1.setBorder(jtpBorder);
		p.add(jtp1);
		jtp2.setText(NUMALL);
		jtp2.setEditable(false);
		jtp2.setBorder(jtpBorder);
		jtp2.setBackground(Color.LIGHT_GRAY);
		jtp2.setFont(jtpFont);
		jtp2.setPreferredSize(draftSize_110);
		p.add(jtp2);
		jtp1.getDocument().addDocumentListener(new JtpListener(jtp1, jtp2));
		addJtpList(label.trim()+"draft1",jtp1);
		addJtpList(label.trim()+"draft2",jtp2);
	}

	private void addJtpList(String label,JTextPane jtp) {
		jtp.addKeyListener(new NumOnlyKeyListener(jtp));
		Jtps.put(label.trim(), jtp);
	}

	private void addEventHandle() {//添加事件监听
		bit.addActionListener(this);
		appear.addActionListener(this);
		sum.addActionListener(this);
		calculator.addActionListener(this);
		clear.addActionListener(this);
		flash.addActionListener(this);
		save.addActionListener(this);
		view.addActionListener(this);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				Lottery.calculateFlag = false;
				Lottery.close();
			}
		});
	}

	public void actionPerformed(ActionEvent ae) {
		String command = ae.getActionCommand();
		if("算码".equals(command)){
			List<SelectNum> sns = getNumList();
			writeResultList(sns);
			save.setEnabled(true);
		}else if("清除".equals(command)){
			clear();
			save.setEnabled(false);
		}else if("刷新".equals(command)){
			flash();
			save.setEnabled(false);
		}else if("保存".equals(command)){
			writeTxt();
			save.setEnabled(false);
		}else if("查看".equals(command)){
			try {
				Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\notepad.exe "+Constants.PATH_CALRESULT);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else if("位数".equals(command)){
			bitMap();
		}else if("见数".equals(command)){
			appearMap();
		}else if("合数".equals(command)){
			sumMap();
		}
	}

	/**
	 * 刷新，重新获取统计数据更新
	 */
	public void flash(){
		if(DataImport.needImport()) return;
		clear();
		periodJtp.setText(DataCalculator.getNextPid()+"");
		Iterator<String> it = Jtps.keySet().iterator();
		String key;
		while(it.hasNext()){
			key = it.next();
			String[] values = DataCalculator.getDataLevel(key);
			if(values != null){
				for(int i = values.length-1; i>0; i--){//出现一次的自动过滤掉
					insertDocument(Jtps.get(key),values[i],getColor(i));
				}
			}
		}
	}
	
	private Color getColor(int i){
		if(i >= colorLv.length){
			return colorLv[colorLv.length-1];
		}
		return colorLv[i];
	}
	
	
	/**
	 * 根据传入的颜色及文字，将文字插入文本域
	 * @param text
	 * @param textColor
	 */
	private void insertDocument(JTextPane jtp, String text, Color textColor){
		SimpleAttributeSet set = new SimpleAttributeSet();
		StyleConstants.setForeground(set, textColor);// 设置文字颜色
		Document doc = jtp.getStyledDocument();
		try {
			doc.insertString(doc.getLength(), text, set);// 插入文字
		} catch (BadLocationException e) {
		}
	}
	

	boolean haveA,haveB,haveC,haveD,haveE,haveF,haveG,needSpare,needHisSpare;
	int empty4 = 0;

	private boolean getNumTreeSet(JTextPane jtp, TreeSet<Integer> treeSet, boolean isFirst4){
		if(jtp.getText().length() > 0){
			String tmp = jtp.getText();
			for(int t = 0 ; t<tmp.length() ;t++){
				treeSet.add(Integer.parseInt(tmp.charAt(t)+""));
			}
			return true;
		}else{
			treeSet.add(0);
			if(isFirst4){
				empty4++;
			}
			return false;
		}
	}

	private List<SelectNum> getNumList(){
		
		List<SelectNum> sns = new ArrayList<SelectNum>();
		
		TreeSet<Integer> A = new TreeSet<Integer>();
		TreeSet<Integer> B = new TreeSet<Integer>();
		TreeSet<Integer> C = new TreeSet<Integer>();
		TreeSet<Integer> D = new TreeSet<Integer>();
		TreeSet<Integer> E = new TreeSet<Integer>();
		TreeSet<Integer> F = new TreeSet<Integer>();
		TreeSet<Integer> G = new TreeSet<Integer>();
		
		haveA = getNumTreeSet(aJtp, A, true);
		haveB = getNumTreeSet(bJtp, B, true);
		haveC = getNumTreeSet(cJtp, C, true);
		haveD = getNumTreeSet(dJtp, D, true);
		haveE = getNumTreeSet(eJtp, E, false);
		haveF = getNumTreeSet(fJtp, F, false);
		haveG = getNumTreeSet(gJtp, G, false);
		
		if(!haveA && !haveB && !haveC && !haveD && !haveE && !haveF && !haveG){
			return sns;
		}
		if(haveE || haveF || haveG){
			needSpare = false;
		}else{
			needSpare = true;
		}
		if(haveA && haveB && haveC && haveD && haveE && haveF && haveG){
			needHisSpare = true;
		}else{
			needHisSpare = false;
		}
		
		initParameter();
		SelectNum sn = null;
		for(int a : A){
			for(int b : B){
				for(int c : C){
					for(int d : D){
						for(int e : E){
							for(int f : F){
								for(int g : G){
									sn = new SelectNum(a,b,c,d,e,f,g,empty4,haveA,haveB,haveC,haveD,haveE,haveF,haveG);
									if(pass(sn)){
										sns.add(sn);
									}
								}
							}
						}
					}
				}
			}
		}
		return sns;
	}
	boolean needsum4;
	int min,max;
	boolean needabSum,needacSum,needadSum,needaeSum,needafSum,needagSum,needbcSum,needbdSum,needbeSum,needbfSum,needbgSum,
	needcdSum,needceSum,needcfSum,needcgSum,needdeSum,needdfSum,needdgSum,needefSum,needegSum,needfgSum;
	boolean needabAppear,needacAppear,needadAppear,needaeAppear,needafAppear,needagAppear,needbcAppear,needbdAppear,needbeAppear,needbfAppear,needbgAppear,
	needcdAppear,needceAppear,needcfAppear,needcgAppear,needdeAppear,needdfAppear,needdgAppear,needefAppear,needegAppear,needfgAppear;
	boolean needabc,needabd,needacd,needbcd,needabcd,needDoubleSum,needSpan;

	List<SelectNum> dropNum = new ArrayList<SelectNum>();//dropNum 为丢弃码，最高级级过滤 == level max
	List<SelectNum> spareNumLv1 = new ArrayList<SelectNum>();//spareNumLv1 为防码，一级过滤， 分配给 头尾不配
	List<SelectNum> spareNumLv2 = new ArrayList<SelectNum>();//spareNumLv2 为防码，二级过滤， 分配给 跨度值
	List<SelectNum> spareNum = new ArrayList<SelectNum>();//spareNum 为防码，最低级过滤 == level min，分配给 两数和数，4位和数，见数防码处理
	List<SelectNum> spareHisNum = new ArrayList<SelectNum>();//spareHisNum 为防码，当7位都有的时候，和历史数据比较

	private void initParameter(){
		dropNum.clear();
		spareNumLv1.clear();
		spareNumLv2.clear();
		spareNum.clear();
		spareHisNum.clear();
		if(sum4MinJtp.getText().length() > 0 && sum4MaxJtp.getText().length()>0){
			needsum4 = true;
			min = Integer.parseInt(sum4MinJtp.getText());
			max = Integer.parseInt(sum4MaxJtp.getText());
		}

		needabSum = abSumJtp.getText().length() > 0;
		needacSum = acSumJtp.getText().length() > 0;
		needadSum = adSumJtp.getText().length() > 0;
		needaeSum = aeSumJtp.getText().length() > 0;
		needafSum = afSumJtp.getText().length() > 0;
		needagSum = agSumJtp.getText().length() > 0;
		needbcSum = bcSumJtp.getText().length() > 0;
		needbdSum = bdSumJtp.getText().length() > 0;
		needbeSum = beSumJtp.getText().length() > 0;
		needbfSum = bfSumJtp.getText().length() > 0;
		needbgSum = bgSumJtp.getText().length() > 0;
		needcdSum = cdSumJtp.getText().length() > 0;
		needceSum = ceSumJtp.getText().length() > 0;
		needcfSum = cfSumJtp.getText().length() > 0;
		needcgSum = cgSumJtp.getText().length() > 0;
		needdeSum = deSumJtp.getText().length() > 0;
		needdfSum = dfSumJtp.getText().length() > 0;
		needdgSum = dgSumJtp.getText().length() > 0;
		needefSum = efSumJtp.getText().length() > 0;
		needegSum = egSumJtp.getText().length() > 0;
		needfgSum = fgSumJtp.getText().length() > 0;
		
		needabAppear = abAppearJtp.getText().length() > 0;
		needacAppear = acAppearJtp.getText().length() > 0;
		needadAppear = adAppearJtp.getText().length() > 0;
		needaeAppear = aeAppearJtp.getText().length() > 0;
		needafAppear = afAppearJtp.getText().length() > 0;
		needagAppear = agAppearJtp.getText().length() > 0;
		needbcAppear = bcAppearJtp.getText().length() > 0;
		needbdAppear = bdAppearJtp.getText().length() > 0;
		needbeAppear = beAppearJtp.getText().length() > 0;
		needbfAppear = bfAppearJtp.getText().length() > 0;
		needbgAppear = bgAppearJtp.getText().length() > 0;
		needcdAppear = cdAppearJtp.getText().length() > 0;
		needceAppear = ceAppearJtp.getText().length() > 0;
		needcfAppear = cfAppearJtp.getText().length() > 0;
		needcgAppear = cgAppearJtp.getText().length() > 0;
		needdeAppear = deAppearJtp.getText().length() > 0;
		needdfAppear = dfAppearJtp.getText().length() > 0;
		needdgAppear = dgAppearJtp.getText().length() > 0;
		needefAppear = efAppearJtp.getText().length() > 0;
		needegAppear = egAppearJtp.getText().length() > 0;
		needfgAppear = fgAppearJtp.getText().length() > 0;
		
		needabc = abcSumJtp.getText().length() > 0;
		needabd = abdSumJtp.getText().length() > 0;
		needacd = acdSumJtp.getText().length() > 0;
		needbcd = bcdSumJtp.getText().length() > 0;
		needabcd = abcdSumJtp.getText().length() > 0;
		needSpan = spanJtp.getText().length() > 0;
		needDoubleSum = doubleSumJtp.getText().length() > 0;
	}

	private boolean pass(SelectNum sn){
		System.out.println(sn.toString());
		//dropNum 为丢弃码，最高级级过滤 == level max 越强硬的条件（获得不重要的码），等级越高，越优先处理
		if(needsum4 && (sn.getSum4() < min || sn.getSum4() > max)){
			dropNum.add(sn);
			return false;
		}
		if(zccm.isSelected() && sn.isDoubleall()){
			dropNum.add(sn);
			return false;
		}
		if(zcec.isSelected() && sn.isDouble2()){
			dropNum.add(sn);
			return false;
		}
		if(zcsc.isSelected() && sn.isDouble3()){
			dropNum.add(sn);
			return false;
		}
		if(wds.isSelected() && sn.isDuishu()){
			dropNum.add(sn);
			return false;
		}
		if(wxds.isSelected() && sn.isXiongdishu()){
			dropNum.add(sn);
			return false;
		}
		
		if(zycm.isSelected() && !sn.isDoubleall()){
			dropNum.add(sn);
			return false;
		}
		if(zyec.isSelected() && !sn.isDouble2()){
			dropNum.add(sn);
			return false;
		}
		if(zysc.isSelected() && !sn.isDouble3()){
			dropNum.add(sn);
			return false;
		}
		if(yds.isSelected() && !sn.isDuishu()){
			dropNum.add(sn);
			return false;
		}
		if(yxds.isSelected() && !sn.isXiongdishu()){
			dropNum.add(sn);
			return false;
		}
		if(needabc && isNotMatch(abcSumJtp, sn.getAbcSum())){
			dropNum.add(sn);
			return false;
		}
		if(needabd && isNotMatch(abdSumJtp, sn.getAbdSum())){
			dropNum.add(sn);
			return false;
		}
		if(needacd && isNotMatch(acdSumJtp, sn.getAcdSum())){
			dropNum.add(sn);
			return false;
		}
		if(needbcd && isNotMatch(bcdSumJtp, sn.getBcdSum())){
			dropNum.add(sn);
			return false;
		}
		
		//spareNumLv1 为防码，一级过滤， 分配给 头尾不配
		if(adMatchJcb.isSelected() && sn.isTouweipei()){
			spareNumLv1.add(sn);
			return false;
		}
		
		//spareNumLv2 为防码，二级过滤， 分配给 跨度值
		if(needSpan && isNotMatch(spanJtp, sn.getKuaduzhi())){
			spareNumLv2.add(sn);
			return false;
		}
		
		//spareNum 为防码，最低级过滤 == level min，分配给 两数和数，4位和数，见数防码处理
		if(needDoubleSum && isNotMatch(doubleSumJtp, sn.getAbSum()) && isNotMatch(doubleSumJtp, sn.getAcSum()) && isNotMatch(doubleSumJtp, sn.getAdSum())
				&& isNotMatch(doubleSumJtp, sn.getBcSum()) && isNotMatch(doubleSumJtp, sn.getBdSum()) && isNotMatch(doubleSumJtp, sn.getCdSum())){
			spareNum.add(sn);
			return false;
		}
		
		if(needabcd && isNotMatch(abcdSumJtp, sn.getAbcdSum())){
			spareNum.add(sn);
			return false;
		}
		
		if(needabSum && isNotMatch(abSumJtp, sn.getAbSum())){
			spareNum.add(sn);
			return false;
		}
		if(needacSum && isNotMatch(acSumJtp, sn.getAcSum())){
			spareNum.add(sn);
			return false;
		}
		if(needadSum && isNotMatch(adSumJtp, sn.getAdSum())){
			spareNum.add(sn);
			return false;
		}
		if(needaeSum && isNotMatch(aeSumJtp, sn.getAeSum())){
			spareNum.add(sn);
			return false;
		}
		if(needafSum && isNotMatch(afSumJtp, sn.getAfSum())){
			spareNum.add(sn);
			return false;
		}
		if(needagSum && isNotMatch(agSumJtp, sn.getAgSum())){
			spareNum.add(sn);
			return false;
		}
		
		if(needbcSum && isNotMatch(bcSumJtp, sn.getBcSum())){
			spareNum.add(sn);
			return false;
		}
		if(needbdSum && isNotMatch(bdSumJtp, sn.getBdSum())){
			spareNum.add(sn);
			return false;
		}
		if(needbeSum && isNotMatch(beSumJtp, sn.getBeSum())){
			spareNum.add(sn);
			return false;
		}
		if(needbfSum && isNotMatch(bfSumJtp, sn.getBfSum())){
			spareNum.add(sn);
			return false;
		}
		if(needbgSum && isNotMatch(bgSumJtp, sn.getBgSum())){
			spareNum.add(sn);
			return false;
		}

		if(needcdSum && isNotMatch(cdSumJtp, sn.getCdSum())){
			spareNum.add(sn);
			return false;
		}
		if(needceSum && isNotMatch(ceSumJtp, sn.getCeSum())){
			spareNum.add(sn);
			return false;
		}
		if(needcfSum && isNotMatch(cfSumJtp, sn.getCfSum())){
			spareNum.add(sn);
			return false;
		}
		if(needcgSum && isNotMatch(cgSumJtp, sn.getCgSum())){
			spareNum.add(sn);
			return false;
		}
		
		if(needdeSum && isNotMatch(deSumJtp, sn.getDeSum())){
			spareNum.add(sn);
			return false;
		}
		if(needdfSum && isNotMatch(dfSumJtp, sn.getDfSum())){
			spareNum.add(sn);
			return false;
		}
		if(needdgSum && isNotMatch(dgSumJtp, sn.getDgSum())){
			spareNum.add(sn);
			return false;
		}
		
		if(needefSum && isNotMatch(efSumJtp, sn.getEfSum())){
			spareNum.add(sn);
			return false;
		}
		if(needegSum && isNotMatch(egSumJtp, sn.getEgSum())){
			spareNum.add(sn);
			return false;
		}
		
		if(needfgSum && isNotMatch(fgSumJtp, sn.getFgSum())){
			spareNum.add(sn);
			return false;
		}
		
		
		if(needabAppear && sn.isHaveA() && sn.isHaveB() && isNotMatch(abAppearJtp, sn.getA()) && isNotMatch(abAppearJtp, sn.getB())){
			spareNum.add(sn);
			return false;
		}
		if(needacAppear && sn.isHaveA() && sn.isHaveC() && isNotMatch(acAppearJtp, sn.getA()) && isNotMatch(acAppearJtp, sn.getC())){
			spareNum.add(sn);
			return false;
		}
		if(needadAppear && sn.isHaveA() && sn.isHaveD() && isNotMatch(adAppearJtp, sn.getA()) && isNotMatch(adAppearJtp, sn.getD())){
			spareNum.add(sn);
			return false;
		}
		if(needaeAppear && sn.isHaveA() && sn.isHaveE() && isNotMatch(aeAppearJtp, sn.getA()) && isNotMatch(aeAppearJtp, sn.getE())){
			spareNum.add(sn);
			return false;
		}
		if(needafAppear && sn.isHaveA() && sn.isHaveF() && isNotMatch(afAppearJtp, sn.getA()) && isNotMatch(afAppearJtp, sn.getF())){
			spareNum.add(sn);
			return false;
		}
		if(needagAppear && sn.isHaveA() && sn.isHaveG() && isNotMatch(agAppearJtp, sn.getA()) && isNotMatch(agAppearJtp, sn.getG())){
			spareNum.add(sn);
			return false;
		}
		
		if(needbcAppear && sn.isHaveB() && sn.isHaveC() && isNotMatch(bcAppearJtp, sn.getB()) && isNotMatch(bcAppearJtp, sn.getC())){
			spareNum.add(sn);
			return false;
		}
		if(needbdAppear && sn.isHaveB() && sn.isHaveD() && isNotMatch(bdAppearJtp, sn.getB()) && isNotMatch(bdAppearJtp, sn.getD())){
			spareNum.add(sn);
			return false;
		}
		if(needbeAppear && sn.isHaveB() && sn.isHaveE() && isNotMatch(beAppearJtp, sn.getB()) && isNotMatch(beAppearJtp, sn.getE())){
			spareNum.add(sn);
			return false;
		}
		if(needbfAppear && sn.isHaveB() && sn.isHaveF() && isNotMatch(bfAppearJtp, sn.getB()) && isNotMatch(bfAppearJtp, sn.getF())){
			spareNum.add(sn);
			return false;
		}
		if(needbgAppear && sn.isHaveB() && sn.isHaveG() && isNotMatch(bgAppearJtp, sn.getB()) && isNotMatch(bgAppearJtp, sn.getG())){
			spareNum.add(sn);
			return false;
		}
		
		if(needcdAppear && sn.isHaveC() && sn.isHaveD() && isNotMatch(cdAppearJtp, sn.getC()) && isNotMatch(cdAppearJtp, sn.getD())){
			spareNum.add(sn);
			return false;
		}
		if(needceAppear && sn.isHaveC() && sn.isHaveE() && isNotMatch(ceAppearJtp, sn.getC()) && isNotMatch(ceAppearJtp, sn.getE())){
			spareNum.add(sn);
			return false;
		}
		if(needcfAppear && sn.isHaveC() && sn.isHaveF() && isNotMatch(cfAppearJtp, sn.getC()) && isNotMatch(cfAppearJtp, sn.getF())){
			spareNum.add(sn);
			return false;
		}
		if(needcgAppear && sn.isHaveC() && sn.isHaveG() && isNotMatch(cgAppearJtp, sn.getC()) && isNotMatch(cgAppearJtp, sn.getG())){
			spareNum.add(sn);
			return false;
		}
		
		if(needdeAppear && sn.isHaveD() && sn.isHaveE() && isNotMatch(deAppearJtp, sn.getD()) && isNotMatch(deAppearJtp, sn.getE())){
			spareNum.add(sn);
			return false;
		}
		if(needdfAppear && sn.isHaveD() && sn.isHaveF() && isNotMatch(dfAppearJtp, sn.getD()) && isNotMatch(dfAppearJtp, sn.getF())){
			spareNum.add(sn);
			return false;
		}
		if(needdgAppear && sn.isHaveD() && sn.isHaveG() && isNotMatch(dgAppearJtp, sn.getD()) && isNotMatch(dgAppearJtp, sn.getG())){
			spareNum.add(sn);
			return false;
		}
		
		if(needefAppear && sn.isHaveE() && sn.isHaveF() && isNotMatch(efAppearJtp, sn.getE()) && isNotMatch(efAppearJtp, sn.getF())){
			spareNum.add(sn);
			return false;
		}
		if(needegAppear && sn.isHaveE() && sn.isHaveG() && isNotMatch(egAppearJtp, sn.getE()) && isNotMatch(egAppearJtp, sn.getG())){
			spareNum.add(sn);
			return false;
		}
		
		if(needfgAppear && sn.isHaveF() && sn.isHaveG() && isNotMatch(fgAppearJtp, sn.getF()) && isNotMatch(fgAppearJtp, sn.getG())){
			spareNum.add(sn);
			return false;
		}
		
		if(needHisSpare && DataCalculator.isAppearedInHistory(sn.toString())){
			spareHisNum.add(sn);
			return false;
		}

		return true;
	}
	
	/**
	 * 对应值 是否 不符合目标码
	 * @param test 目标吗
	 * @param value 对应值
	 * @return 是 - 进入防码或丢弃  否 - 进入主码
	 */
	private boolean isNotMatch(JTextPane test,int value){
		return test.getText().indexOf(""+value) == -1;
	}
	
	public static String doubleTrans(double d){
		if(Math.round(d)-d==0){
			return String.valueOf((long)d);
		}
		return DF.format(d);
	}
	
	private void writeResultList(List<SelectNum> sns){
		StringBuilder sb = new StringBuilder();
		sb.append("海南体彩第("+periodJtp.getText()+")期预测，祝君中奖！");
		sb.append("\r\n=======================================");
		sb.append("\r\nA："+aJtp.getText());
		sb.append("  B："+bJtp.getText());
		sb.append("  C："+cJtp.getText());
		sb.append("  D："+dJtp.getText());
		sb.append("  E："+eJtp.getText());
		sb.append("  F："+fJtp.getText());
		sb.append("  G："+gJtp.getText());
		
		if(needsum4)
			sb.append("\r\n总值:从"+sum4MinJtp.getText()+"到"+sum4MaxJtp.getText());
		
		if(zccm.isSelected()) sb.append("\r\n只除重码");
		if(zcec.isSelected()) sb.append("\r\n只除二重");
		if(zcsc.isSelected()) sb.append("\r\n只除三重");
		if(wds.isSelected()) sb.append("\r\n无对数");
		if(wxds.isSelected()) sb.append("\r\n无兄弟数");
		
		if(zycm.isSelected()) sb.append("\r\n只要重码");
		if(zyec.isSelected()) sb.append("\r\n只要二重");
		if(zysc.isSelected()) sb.append("\r\n只要三重");
		if(yds.isSelected()) sb.append("\r\n有对数");
		if(yxds.isSelected()) sb.append("\r\n有兄弟数");
		
		if(needabAppear) sb.append("\r\nABS："+abAppearJtp.getText());
		if(needacAppear) sb.append("\r\nACS："+acAppearJtp.getText());
		if(needadAppear) sb.append("\r\nADS："+adAppearJtp.getText());
		if(needaeAppear) sb.append("\r\nAES："+aeAppearJtp.getText());
		if(needafAppear) sb.append("\r\nAFS："+afAppearJtp.getText());
		if(needagAppear) sb.append("\r\nAGS："+agAppearJtp.getText());
		
		if(needbcAppear) sb.append("\r\nBCS："+bcAppearJtp.getText());
		if(needbdAppear) sb.append("\r\nBDS："+bdAppearJtp.getText());
		if(needbeAppear) sb.append("\r\nBES："+beAppearJtp.getText());
		if(needbfAppear) sb.append("\r\nBFS："+bfAppearJtp.getText());
		if(needbgAppear) sb.append("\r\nBGS："+bgAppearJtp.getText());
		
		if(needcdAppear) sb.append("\r\nCDS："+cdAppearJtp.getText());
		if(needceAppear) sb.append("\r\nCES："+ceAppearJtp.getText());
		if(needcfAppear) sb.append("\r\nCFS："+cfAppearJtp.getText());
		if(needcgAppear) sb.append("\r\nCGS："+cgAppearJtp.getText());
		
		if(needdeAppear) sb.append("\r\nDES："+deAppearJtp.getText());
		if(needdfAppear) sb.append("\r\nDFS："+dfAppearJtp.getText());
		if(needdgAppear) sb.append("\r\nDGS："+dgAppearJtp.getText());
		
		if(needefAppear) sb.append("\r\nEFS："+efAppearJtp.getText());
		if(needegAppear) sb.append("\r\nEGS："+egAppearJtp.getText());
		if(needfgAppear) sb.append("\r\nFGS："+fgAppearJtp.getText());
		
		if(needabSum) sb.append("\r\nABH："+abSumJtp.getText());
		if(needacSum) sb.append("\r\nACH："+acSumJtp.getText());
		if(needadSum) sb.append("\r\nADH："+adSumJtp.getText());
		if(needaeSum) sb.append("\r\nAEH："+aeSumJtp.getText());
		if(needafSum) sb.append("\r\nAFH："+afSumJtp.getText());
		if(needagSum) sb.append("\r\nAGH："+agSumJtp.getText());
		
		if(needbcSum) sb.append("\r\nBCH："+bcSumJtp.getText());
		if(needbdSum) sb.append("\r\nBDH："+bdSumJtp.getText());
		if(needbeSum) sb.append("\r\nBEH："+beSumJtp.getText());
		if(needbfSum) sb.append("\r\nBFH："+bfSumJtp.getText());
		if(needbgSum) sb.append("\r\nBGH："+bgSumJtp.getText());
		
		if(needcdSum) sb.append("\r\nCDH："+cdSumJtp.getText());
		if(needceSum) sb.append("\r\nCEH："+ceSumJtp.getText());
		if(needcfSum) sb.append("\r\nCFH："+cfSumJtp.getText());
		if(needcgSum) sb.append("\r\nCGH："+cgSumJtp.getText());
		
		if(needdeSum) sb.append("\r\nDEH："+deSumJtp.getText());
		if(needdfSum) sb.append("\r\nDFH："+dfSumJtp.getText());
		if(needdgSum) sb.append("\r\nDGH："+deSumJtp.getText());
		
		if(needefSum) sb.append("\r\nEFH："+efSumJtp.getText());
		if(needegSum) sb.append("\r\nEGH："+egSumJtp.getText());
		if(needfgSum) sb.append("\r\nFGH："+fgSumJtp.getText());
		
		if(needabc) sb.append("\r\nABCH："+abcSumJtp.getText());
		if(needabd) sb.append("\r\nABDH："+abdSumJtp.getText());
		if(needacd) sb.append("\r\nACDH："+acdSumJtp.getText());
		if(needbcd) sb.append("\r\nBCDH："+bcdSumJtp.getText());
		if(needabcd) sb.append("\r\nABCDH："+abcdSumJtp.getText());
		if(needDoubleSum) sb.append("\r\n两数和值："+doubleSumJtp.getText());
		if(needSpan) sb.append("\r\n跨度值："+spanJtp.getText());
		if(adMatchJcb.isSelected()) sb.append("\r\n头尾不配");
		
		sb.append("\r\n=======================================");
		if(zmy.getText().length()>0){
			double total = 0;
			double per = Double.parseDouble(zmy.getText());
			total = sns.size()*per;
			sb.append("\r\n主码每注："+doubleTrans(per)+"元  主码总额："+ doubleTrans(total)+"元");
			if(needSpare && fmy.getText().length()>0){
				per = Double.parseDouble(fmy.getText());
				int fmsize = 0;
				if(spareNum.size() > 0){
					fmsize = spareNum.size();
				}
				if(adMatchBuyJcb.isSelected() && spareNumLv1.size() > 0){
					fmsize += spareNumLv1.size();
				}
				if(spanBuyJcb.isSelected() && spareNumLv2.size() > 0){
					fmsize += spareNumLv2.size();
				}
				total += fmsize*per;
				sb.append("\r\n防码每注："+doubleTrans(per)+"元  防码总额："+ doubleTrans(fmsize*per)+"元");
			}
			sb.append("\r\n总金额："+ doubleTrans(total)+"元");
			sb.append("\r\n_______________________________________");
		}
		sb.append("\r\n满足条件的主码总数为："+sns.size());
		int line = 5;
		if(needSpare) line = 10;
		for(int i = 0 ; i < sns.size() ; i++){
			if(i%line==0){
				sb.append("\r\n");
			}
			sb.append(sns.get(i).toString()+"  ");
		}
		if(needSpare && spareNum.size() > 0){
			sb.append("\r\n_______________________________________");
			sb.append("\r\n不满足见数，和数，多码和值条件的防码总数为："+spareNum.size());
			for(int i = 0 ; i < spareNum.size() ; i++){
				if(i%line==0){
					sb.append("\r\n");
				}
				sb.append(spareNum.get(i).toString()+"   ");
			}
		}
		if(needSpare && adMatchJcb.isSelected()){
			sb.append("\r\n_______________________________________");
			sb.append("\r\n");
			if(adMatchBuyJcb.isSelected()){
				sb.append("(购买)");
			}else{
				sb.append("(不购买)");
			}
			sb.append("不满足头尾不配的防码总数为："+spareNumLv1.size());
			for(int i = 0 ; i < spareNumLv1.size() ; i++){
				if(i%line==0){
					sb.append("\r\n");
				}
				sb.append(spareNumLv1.get(i).toString()+"   ");
			}
		}
		if(needSpare && needSpan){
			sb.append("\r\n_______________________________________");
			sb.append("\r\n");
			if(spanBuyJcb.isSelected()){
				sb.append("(购买)");
			}else{
				sb.append("(不购买)");
			}
			sb.append("满足跨度值的防码总数为："+spareNumLv2.size());
			for(int i = 0 ; i < spareNumLv2.size() ; i++){
				if(i%line==0){
					sb.append("\r\n");
				}
				sb.append(spareNumLv2.get(i).toString()+"   ");
			}
		}
		if(needHisSpare){
			sb.append("\r\n_______________________________________");
			sb.append("\r\n");
			sb.append("(不购买)");
			sb.append("满足历史出现条件的防码总数为："+spareHisNum.size());
			for(int i = 0 ; i < spareHisNum.size() ; i++){
				if(i%line==0){
					sb.append("\r\n");
				}
				sb.append(spareHisNum.get(i).toString()+"   ");
			}
		}
		
		text.setText("");
		text.append(sb.toString());
		text.setCaretPosition(0);
	}
	
	private void bitMap(){
		JtpMap(aJtp, aDraftJtp2);
		JtpMap(bJtp, bDraftJtp2);
		JtpMap(cJtp, cDraftJtp2);
		JtpMap(dJtp, dDraftJtp2);
		JtpMap(eJtp, eDraftJtp2);
		JtpMap(fJtp, fDraftJtp2);
		JtpMap(gJtp, gDraftJtp2);
	}
	
	private void appearMap(){
		JtpMap(abAppearJtp, abDraftJtp2);
		JtpMap(acAppearJtp, acDraftJtp2);
		JtpMap(adAppearJtp, adDraftJtp2);
		JtpMap(aeAppearJtp, aeDraftJtp2);
		JtpMap(afAppearJtp, afDraftJtp2);
		JtpMap(agAppearJtp, agDraftJtp2);
		JtpMap(bcAppearJtp, bcDraftJtp2);
		JtpMap(bdAppearJtp, bdDraftJtp2);
		JtpMap(beAppearJtp, beDraftJtp2);
		JtpMap(bfAppearJtp, bfDraftJtp2);
		JtpMap(bgAppearJtp, bgDraftJtp2);
		JtpMap(cdAppearJtp, cdDraftJtp2);
		JtpMap(ceAppearJtp, ceDraftJtp2);
		JtpMap(cfAppearJtp, cfDraftJtp2);
		JtpMap(cgAppearJtp, cgDraftJtp2);
		JtpMap(deAppearJtp, deDraftJtp2);
		JtpMap(dfAppearJtp, dfDraftJtp2);
		JtpMap(dgAppearJtp, dgDraftJtp2);
		JtpMap(efAppearJtp, efDraftJtp2);
		JtpMap(egAppearJtp, egDraftJtp2);
		JtpMap(fgAppearJtp, fgDraftJtp2);
	}
	
	private void sumMap(){
		JtpMap(abSumJtp, abDraftJtp2);
		JtpMap(acSumJtp, acDraftJtp2);
		JtpMap(adSumJtp, adDraftJtp2);
		JtpMap(aeSumJtp, aeDraftJtp2);
		JtpMap(afSumJtp, afDraftJtp2);
		JtpMap(agSumJtp, agDraftJtp2);
		JtpMap(bcSumJtp, bcDraftJtp2);
		JtpMap(bdSumJtp, bdDraftJtp2);
		JtpMap(beSumJtp, beDraftJtp2);
		JtpMap(bfSumJtp, bfDraftJtp2);
		JtpMap(bgSumJtp, bgDraftJtp2);
		JtpMap(cdSumJtp, cdDraftJtp2);
		JtpMap(ceSumJtp, ceDraftJtp2);
		JtpMap(cfSumJtp, cfDraftJtp2);
		JtpMap(cgSumJtp, cgDraftJtp2);
		JtpMap(deSumJtp, deDraftJtp2);
		JtpMap(dfSumJtp, dfDraftJtp2);
		JtpMap(dgSumJtp, dgDraftJtp2);
		JtpMap(efSumJtp, efDraftJtp2);
		JtpMap(egSumJtp, egDraftJtp2);
		JtpMap(fgSumJtp, fgDraftJtp2);
	}
	
	
	private void JtpMap(JTextPane jtp1,JTextPane jtp2){
		jtp1.setText(jtp2.getText());
	}
	
	private void writeTxt(){
		FileWriter fw = null;
		PrintWriter pw = null;
		FileReader fr = null;
		BufferedReader br = null;
		List<String> content = new ArrayList<String>();
		try {
			File ma = new File(Constants.PATH_CALRESULT);
			if(!ma.exists()){
				ma.createNewFile();
			}
			fr = new FileReader(ma);
			br = new BufferedReader(fr);
			String s = null;
			while ((s = br.readLine()) != null) {
				content.add(s);
			}
			fw = new FileWriter(ma,false);
			pw = new PrintWriter(fw);
			pw.println(text.getText());
			pw.println(">>>>>>>>>>>>>>>"+Constants.YMDHMS.format(new Date())+"<<<<<<<<<<<<<<<");
			pw.println("");
			for(String t : content){
				pw.println(t);
			}
			pw.flush();
		} catch (IOException e) {
			DataUtil.writeLog("写入计算结果出错", e);
		}finally{
			try{if(pw != null);pw.close();}catch (Exception e){}
			try{if(fw != null);fw.close();}catch (Exception e){}
			try{if(br != null);br.close();}catch (Exception e){}
			try{if(fr != null);fr.close();}catch (Exception e){}
		}
	}
	
	private void clear(){
		zccm.setSelected(false);
		zcec.setSelected(false);
		zcsc.setSelected(false);
		wds.setSelected(false);
		wxds.setSelected(false);
		
		zycm.setSelected(false);
		zyec.setSelected(false);
		zysc.setSelected(false);
		yds.setSelected(false);
		yxds.setSelected(false);
		
		adMatchJcb.setSelected(false);
		adMatchBuyJcb.setSelected(false);
		spanBuyJcb.setSelected(false);
		
		Iterator<String> it = Jtps.keySet().iterator();
		String key;
		while(it.hasNext()){
			key = it.next();
			if(key.endsWith("draft2")){
				Jtps.get(key).setText(NUMALL);
			}else{
				Jtps.get(key).setText("");
			}
		}
		
		zmy.setText("");
		fmy.setText("");
	}
	
	public class NumOnlyKeyListener extends KeyAdapter {
		JTextPane jtp;
		
		public NumOnlyKeyListener(JTextPane jtp) {
			super();
			this.jtp = jtp;
		}

		@Override
		public void keyTyped(KeyEvent e) {
			char keyChar=e.getKeyChar();
			if (keyChar>=KeyEvent.VK_0 && keyChar<=KeyEvent.VK_9) {
				if(jtp.getText().indexOf(keyChar) != -1){
					e.consume();
				}
			} else {
				e.consume();
			}
		}
		
	}

	class JtpListener implements DocumentListener {
		JTextPane parent;
		JTextPane son;
		
		public JtpListener(JTextPane parent) {
			this.parent = parent;
		}
		
		public JtpListener(JTextPane parent,JTextPane son) {
			this.parent = parent;
			this.son = son;
		}

		public void changedUpdate(DocumentEvent documentEvent) {
			deal();
		}

		public void insertUpdate(DocumentEvent documentEvent) {
			deal();
		}

		public void removeUpdate(DocumentEvent documentEvent) {
			deal();
		}

		private void deal() {
			String pt = parent.getText();
			String st = NUMALL;
			for(int i = 0; i < pt.length(); i++){
				st = st.replaceFirst(""+pt.charAt(i), "");
			}
			son.setText(st);
		}
		
	}
	

}
